package kr.saeildamil.androidstudty.ch14;

import kr.saeildamil.androidstudty.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.AttributeSet;
import android.widget.EditText;

public class SoundEditWidgetAttr extends EditText {
	SoundPool mPool;
	int mSound;
	int mClick1,mClick2;
	float mVolume=1.0f;
	float mSpeed=1.0f;
	
	private void init(Context context, AttributeSet attrs) {
		mPool=new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
		mClick1=mPool.load(context, R.raw.click, 1);
		mClick2=mPool.load(context, R.raw.click2, 1);
		mSound=mClick1;
		
		if(attrs!=null){
			TypedArray ar=context.obtainStyledAttributes(attrs,R.styleable.SoundEditWidgetAttr);
			mVolume=ar.getFloat(R.styleable.SoundEditWidgetAttr_volume, 1.0f);
			mSound=ar.getInt(R.styleable.SoundEditWidgetAttr_sound, mClick1);
			mSpeed=ar.getFloat(R.styleable.SoundEditWidgetAttr_speed, 1.0f);
			ar.recycle();	
		}
	}

	@Override
	protected void onTextChanged(CharSequence text, int start,
			int lengthBefore, int lengthAfter) {
		// TODO Auto-generated method stub
		super.onTextChanged(text, start, lengthBefore, lengthAfter);
		if(mPool!=null){
			mPool.play(mSound, mVolume, mVolume, 0, 0, mSpeed);
		}
	}

	public SoundEditWidgetAttr(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context,attrs);
	}

	
	public SoundEditWidgetAttr(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context,attrs);
	}

	public SoundEditWidgetAttr(Context context) {
		super(context);
		init(context,null);
	}
	
}
